package com.ray.study.sample.test.common.java8;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * description
 *
 * @author r.shi 2020/04/03 16:56
 */
public class LocalDateSample {

    public static void DateToString() {
        String dateTimeFormatPattern = "yyyyMMdd";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern);

        LocalDate localLastDay = LocalDate.now().minusDays(1);
        String lastDay  = localLastDay.format(formatter);
        System.out.println("lastDay:"+lastDay);
    }

    public static void stringToDate() {
        String dateTimeFormatPattern = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern);

        String targetDateTime ="2020-04-03 01:29:22.128000";
        String stringDate = targetDateTime.substring(0,dateTimeFormatPattern.length());

        LocalDate localDate = LocalDate.parse(stringDate,formatter);
        System.out.println("localDate："+localDate);

        LocalDateTime localDateTime = LocalDateTime.parse(stringDate,formatter);
        System.out.println("localDateTime："+localDateTime);
    }



    public static void getDaysOfLastWeek() {
        LocalDate now = LocalDate.now();

        LocalDate mondayOfLastWeek = now.minusWeeks(1).with(DayOfWeek.MONDAY);
        System.out.println("mondayOfLastWeek:"+mondayOfLastWeek);

        LocalDate sundayOfLastWeek = now.minusWeeks(1).with(DayOfWeek.SUNDAY);
        System.out.println("sundayOfLastWeek:"+sundayOfLastWeek);
    }


    public static void toSqlDate() {
        LocalDate now = LocalDate.now();
        Date date = Date.valueOf(now);

        System.out.println("date:"+date);
    }

    public static void toSqlTimestamp() {
        LocalDateTime localDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(localDateTime);
        System.out.println("localDateTime:"+localDateTime);
    }

}
